data "aws_ami" "ubuntu2204" {

    most_recent = true

    filter {
        name   = "name"
        values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"]
    }

    filter {
        name = "virtualization-type"
        values = ["hvm"]
    }

    owners = ["099720109477"]
}

resource "aws_vpc" "device_<%= obj['ID'] %>" {
    cidr_block = "<%= provision['CIDR'] ? provision['CIDR'] : '10.0.0.0/16'%>"

    tags = {
        Name = "<%= obj['NAME'] %>_vpc"
    }
}


resource "aws_subnet" "device_<%= obj['ID'] %>" {
    vpc_id     = aws_vpc.device_<%= obj['ID'] %>.id
    cidr_block = "<%= provision['CIDR'] ? provision['CIDR'] : '10.0.0.0/16'%>"

    map_public_ip_on_launch = true

    tags = {
        Name = "<%= obj['NAME'] %>_subnet"
    }

    availability_zone      = local.my_zones[0]
}

<% if !ceph_vars.nil? && ceph_vars['ceph_hci'] %>
<%# Additional CIDR block for HCI %>
resource "aws_vpc_ipv4_cidr_block_association" "hci_cidr_<%= obj['ID'] %>" {
    vpc_id     = aws_vpc.device_<%= obj['ID'] %>.id

    cidr_block = "<%= provision['HCI_CIDR'] ? provision['HCI_CIDR'] : '10.1.0.0/16'%>"
}

resource "aws_subnet" "device_<%= obj['ID'] %>_ceph" {
    vpc_id     = aws_vpc.device_<%= obj['ID'] %>.id
    cidr_block = "<%= provision['HCI_CIDR'] ? provision['HCI_CIDR'] : '10.1.0.0/16'%>"

    tags = {
        Name = "<%= obj['NAME'] %>_ceph_subnet"
    }

    availability_zone      = local.my_zones[0]

    depends_on = [ aws_vpc_ipv4_cidr_block_association.hci_cidr_<%= obj['ID'] %> ]
}
<% end %>

resource "aws_internet_gateway" "device_<%= obj['ID'] %>" {
    vpc_id = aws_vpc.device_<%= obj['ID'] %>.id

    tags = {
        Name = "<%= obj['NAME'] %>_gateway"
    }
}

resource "aws_route" "device_<%= obj['ID'] %>" {
    route_table_id         = aws_vpc.device_<%= obj['ID'] %>.main_route_table_id
    destination_cidr_block = "0.0.0.0/0"
    gateway_id             = aws_internet_gateway.device_<%= obj['ID'] %>.id
}

resource "aws_security_group" "device_<%= obj['ID'] %>_all" {
  name        = "allow_all"
  description = "Allow all traffic"
  vpc_id     = aws_vpc.device_<%= c['ID'] %>.id

  ingress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }

  tags = {
    Name = "device_<%= obj['ID'] %>_all"
  }
}

# Following data is used in host and subnet to filter out AWS av.zones
# where the required instance type is not avialble.
# (Like c5.metal is not avialble in us-east-1e)

data "aws_availability_zones" "available" {}

data "aws_ec2_instance_type_offerings" "available" {

    for_each=toset(data.aws_availability_zones.available.names)

    filter {
        name   = "instance-type"
        values = ["<%= provision['INSTANCETYPE'] %>"]
    }

    filter {
        name   = "location"
        values = ["${each.key}"]
    }

    location_type = "availability-zone"
}

locals {
    my_zones = keys({ for az, details in data.aws_ec2_instance_type_offerings.available :
                      az => details.instance_types if length(details.instance_types) != 0 })
}

